// work1.cpp : Defines the exported functions for the DLL application.
//

#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#include "stdafx.h"


int ID[2];
HANDLE work;



void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Print information about the memory usage of the process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
        printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
        printf( "\tPeakWorkingSetSize: 0x%08X\n", 
                  pmc.PeakWorkingSetSize );
        printf( "\tWorkingSetSize: %d\n", pmc.WorkingSetSize/1024 );
        printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
        printf( "\tPeakPagefileUsage: 0x%08X\n", 
                  pmc.PeakPagefileUsage );
    }

    CloseHandle( hProcess );
}




int SendKeys(HWND hWnd, TCHAR *str1, TCHAR *str2,int num1)
{
		
 HWND workbenchHandle = FindWindow(str1,str2);//FindWindow(str1, str2);
		//HWND workbenchHandle = GetDlgItem(FindWindow("Notepad",0),15);//FindWindow(str1, str2);
		//MessageBox(NULL, str1, TEXT("handle"), MB_OK);
        // Verify that Calculator is a running process.
        if (workbenchHandle == NULL)
        {
       
		    MessageBox(NULL, str1, TEXT("Error - Program not running"), MB_OK);
            return 0;
        }
		
       
    //workbench to foreground  
	//SetForegroundWindow(workbenchHandle);
		Sleep(2000);
   
	
	keybd_event(VK_TAB,0x8f,0 , 0); // Tab Press
	Sleep(1000);
   
	keybd_event(VK_RETURN,0x9c,0,0);
	Sleep(5000);
	//MessageBox(NULL, str1, TEXT("sent tab"), MB_OK);
	HWND ProceedHandle2 = FindWindow( TEXT("WindowsForms10.Window.8.app.0.33c0d9d"),"");
	if (ProceedHandle2 == NULL)
    {
              MessageBox(NULL,  TEXT("Proceed is not running."), TEXT("Error - Program not running"), MB_OK);
	 
            return 1;
     }
		
	Sleep(500);
        // Make the popup message the foreground application and send it
        // a set of key presses.
   //    SetForegroundWindow(ProceedHandle2);
	   Sleep(500);
	keybd_event(VK_TAB,0x8f,0 , 0);
	//keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0);
    //MessageBox(NULL, "tabbb", TEXT("handle1"), MB_OK);
	Sleep(500);
	//SetForegroundWindow(ProceedHandle2);
	keybd_event(VK_TAB,0x8f,0 , 0);
	Sleep(500);
	//keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0);

	//MessageBox(NULL, "tabbb 222", TEXT("handle1"), MB_OK);
	
	//SetForegroundWindow(ProceedHandle2);
		//keybd_event(VK_RETURN,0x0D, KEYEVENTF_KEYUP,0); // Tab Release
	//	SendMessage(ProceedHandle2, VK_TAB,0x09,  0);		
		//SendMessage(ProceedHandle2, VK_TAB,0x09,  0);

		//SendMessage(ProceedHandle2, VK_TAB,0x8f, NULL);
		//SendMessage(ProceedHandle2, VK_TAB,0x8f,  KEYEVENTF_KEYUP);
		//SendMessage(ProceedHandle2, VK_TAB,0x8f, NULL);
		//SendMessage(ProceedHandle2, VK_RETURN,0x9c, NULL);
		//SendMessage(ProceedHandle2, WM_KEYDOWN,VK_TAB, NULL);
		keybd_event(VK_RETURN,0x9c,0,0);


		Sleep(1000);

		 ProceedHandle2 = FindWindow( TEXT("WindowsForms10.Window.8.app.0.33c0d9d"),"Tests Running");
		{
			ShowWindow(ProceedHandle2, FALSE);
		}
        return 0;
      
}
void runWB()
{
    STARTUPINFO procSt;
    PROCESS_INFORMATION procInfo;
    STARTUPINFO procSt2;
    PROCESS_INFORMATION procInfo2;

    memset(&procSt, 0, sizeof(procSt));
    memset(&procInfo, 0, sizeof(procInfo));
    memset(&procSt2, 0, sizeof(procSt2));
    memset(&procInfo2, 0, sizeof(procInfo2));

    procSt.dwFlags = STARTF_USESHOWWINDOW;
    procSt.wShowWindow = SW_HIDE;
    procSt2.dwFlags = STARTF_USESHOWWINDOW;
    procSt2.wShowWindow = SW_HIDE;

    procSt.cb = sizeof(procSt);
    procSt2.cb = sizeof(procSt2);

    //Path points to the workbench executable.
    printf("Starting workbench program\n");
    if (!CreateProcess("C:\\Mosaic\\NovaBench\\nb3.exe", NULL, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &procSt, &procInfo)) //CREATE_NO_WINDOW
    {
        printf("Couldn't run it\n");
    }
  
        Sleep(1000);
		//class = WindowsForms10.Window.8.app.0.378734a/WindowsForms10.Window.8.app.0.33c0d9d
//		SendKeys(NULL, "WindowsForms10.Window.8.app.0.378734a", "NovaBench", 1);

	Sleep(1000);
     

    ID[0] = procInfo.dwProcessId;
   
    work = procInfo.hProcess;

}


 

int performFunctions(void)
{
	
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    //Run workbench.
  //  runWB();
//	Sleep(2000);
	


        PrintMemoryInfo(ID[0]);
        //Sleep(3000);
		
   
		printf("id = %d" , ID[0]);
  //  TerminateProcess(work, 0);
    //printf("Workbench program exited\n");

    return ID[0];
}
